﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NAudio.Wave;
using System.Configuration;


namespace HPKaraoke
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public string ZingOfflinePath { get; set; }

        PlayListGUI playlist = new PlayListGUI();

        public MainWindow()
        {
            InitializeComponent();
            
        }

        private void HPKaraoke_Loaded(object sender, RoutedEventArgs e)
        {
            ucSentence1.BackgroundText.Stroke = Brushes.Black;
            ucSentence1.BackgroundText.Fill = Brushes.WhiteSmoke;
            ucSentence1.AnimationText.Stroke = Brushes.Yellow;
            ucSentence1.AnimationText.Fill = Brushes.Blue;
            ucSentence1.BackgroundText.StrokeThickness = 2;
            ucSentence1.AnimationText.StrokeThickness = 2;
            ucSentence1.BackgroundText.Bold = true;
            ucSentence1.AnimationText.Bold = true;
            ucSentence1.LyricFontSize = 80;

            ucSentence2.BackgroundText.Stroke = Brushes.Black;
            ucSentence2.BackgroundText.Fill = Brushes.WhiteSmoke;
            ucSentence2.AnimationText.Stroke = Brushes.Yellow;
            ucSentence2.AnimationText.Fill = Brushes.Blue;
            ucSentence2.BackgroundText.StrokeThickness = 2;
            ucSentence2.AnimationText.StrokeThickness = 2;
            ucSentence2.BackgroundText.Bold = true;
            ucSentence2.AnimationText.Bold = true;
            ucSentence2.LyricFontSize = 80;

            ucSentence2.BackgroundText.SetCallBackOnTextChanged(ChangePositionOfLyric);

            playlist.SetControlDisplayLyrics(ucSentence1, ucSentence2);
            playlist.SetControlDisplayListSongs(lbListNumber);

            LibraryController.LoadAllSongInLibrary(ConfigurationSettings.AppSettings["ZingDataPath"]);

        }

		private void HPKaraoke_Closed(object sender, EventArgs e)
        {
            LibraryController.Dispose();
        }

        int number = 0;

        private void lbNumber_KeyDown(object sender, KeyEventArgs e)
        {
            int keyPressed = 0;
            bool NumberKey = false;
            #region switch
            switch (e.Key)
            {
                case Key.D0:
                case Key.NumPad0:
                    keyPressed = 0; NumberKey = true;
                    break;
                case Key.D1:
                case Key.NumPad1:
                    keyPressed = 1; NumberKey = true;
                    break;
                case Key.D2:
                case Key.NumPad2:
                    keyPressed = 2; NumberKey = true;
                    break;
                case Key.D3:
                case Key.NumPad3:
                    keyPressed = 3; NumberKey = true;
                    break;
                case Key.D4:
                case Key.NumPad4:
                    keyPressed = 4; NumberKey = true;
                    break;
                case Key.D5:
                case Key.NumPad5:
                    keyPressed = 5; NumberKey = true;
                    break;
                case Key.D6:
                case Key.NumPad6:
                    keyPressed = 6; NumberKey = true;
                    break;
                case Key.D7:
                case Key.NumPad7:
                    keyPressed = 7; NumberKey = true;
                    break;
                case Key.D8:
                case Key.NumPad8:
                    keyPressed = 8; NumberKey = true;
                    break;
                case Key.D9:
                case Key.NumPad9:
                    keyPressed = 9; NumberKey = true;
                    break;
                case Key.Back:
                    keyPressed = -1; NumberKey = true;number = number/10;
                    break;
                case Key.Enter:
                    PlayCurrentSong();
                    lbNumber.Visibility = System.Windows.Visibility.Hidden;
                    break;
                default: NumberKey = false;
                    break;
            }
            #endregion

            if (NumberKey == true)
            {
                lbNumber.Visibility = System.Windows.Visibility.Visible;
                if (keyPressed != -1)
                {
                    lbNumber.Text = string.Format("{0:000000}", (int.Parse(lbNumber.Text.ToString()) * 10 + keyPressed) % 1000000);
                }
                else
                {
                    lbNumber.Text = string.Format("{0:000000}", (int.Parse(lbNumber.Text.ToString()) / 10 + keyPressed + 1) % 1000000);
                }

                if (keyPressed != -1)
                    number = (number * 10 + keyPressed);

                if (number >= 1000000)
                {
                    number = int.Parse(lbNumber.Text as string);
                }

                SongGUI temp = null;
                temp = LibraryController.FindByID(number);
                
                if (temp != null)
                {
                    lbSearchSong.Text = temp.song.Information.Title;
                    playlist.Add(temp);
                    
                    //playlist.PlayCurrentSong();
                }
                else
                {
                    lbSearchSong.Text = "Not Found";
                }
            }
        }

        private void PlayCurrentSong()
        {
            number = 0;
            lbNumber.Text = string.Format("{0:000000}", 0);
            lbSearchSong.Text = "";
            playlist.PlayCurrentSong();
        }

        private void lvitemExit_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void lvitemExit_Selected(object sender, RoutedEventArgs e)
        {

            MyMessageBox msg = new MyMessageBox();
            Grid.SetRow(msg, 0);
            Grid.SetRowSpan(msg, 3);
            mainGrid.Children.Add(msg);
            msg.WindowParent = this;
        }

        private void lvitemSearch_Selected(object sender, RoutedEventArgs e)
        {
            SearchForm searchControl = new SearchForm();
            searchControl.Width = 820;
            Grid.SetRow(searchControl, 0);
            Grid.SetRowSpan(searchControl, 3);
            mainGrid.Children.Add(searchControl);
            searchControl.OnSearchEnded += new RoutedEventHandler(searchControl_OnSearchEnded);
            searchControl.OnDoubleClickItem += new RoutedEventHandler(searchControl_OnDoubleClickItem);        
        }

        void searchControl_OnDoubleClickItem(object sender, RoutedEventArgs e)
        {
            this.AddSongToPlaylistByID(((e as RoutedEventSongInfoDoubleClickArg).Value as SongInfo).ID);
        }

        void searchControl_OnSearchEnded(object sender, RoutedEventArgs e)
        {
            mainGrid.Children.Remove(sender as SearchForm);
            Menu.UnselectAll();
        }

        private void ChangePositionOfLyric()
        {
            Thickness oldMargin = ucSentence2.Margin;
            ucSentence2.Margin = new Thickness(oldMargin.Left, oldMargin.Top, ucSentence2.BackgroundText.RenderSize.Width + 10, oldMargin.Bottom);
        }

        private void btnForward_Click(object sender, RoutedEventArgs e)
        {
            playlist.NextSong();
            playlist.PlayCurrentSong();
        }

        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void btnBackward_Click(object sender, RoutedEventArgs e)
        {
            //playlist.PreviousSong();
            //playlist.PlayCurrentSong();
        }

        internal void AddSongToPlaylistByID(int p)
        {
            SongGUI s = LibraryController.FindByID(p);
            if (s != null)
            {
                playlist.Add(s);
                playlist.PlayCurrentSong();
            }
            mainGrid.Children.Remove(this.FindName("searchControl") as SearchForm);
        }

        private void HPKaraoke_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {

        }
    }
}
